package com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.order.act.OrderWaitCommentActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.order.act.OrderWaitPayActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.order.act.OrderWaitServiceActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.order.act.OrderWaitTakeDeliveryActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/12.
 * Desc   :
 */

public class OrderAdapter extends CommonAdapter<OrderBean> {

    private int type;

    public OrderAdapter(Context context, int layoutId, List<OrderBean> datas, int type) {
        super(context, layoutId, datas);
        this.type = type;

    }

    @Override
    protected void convert(final ViewHolder holder, OrderBean bean, int position) {
        holder.setText(R.id.tv_order_num, String.format(ResourcesUtils.getString(R.string.order_num), bean.getOrder_num()));
        //订单状态0：下单成功；1：以发货/正在进行中；2:已完成/已收货；3：退单；4退单成功;5待付款；6待评价
        switch (bean.getState()) {
            case "0":
            case "1":
            case "2":
                //type 	1:项目;2:产品
                if (bean.getType().equals("1")) {
                    holder.setVisible(R.id.ll_wait_service, true);
                } else {
                    holder.setVisible(R.id.ll_wait_confirm, true);
                }
                break;
            case "3":
                break;
            case "4":
                break;
            case "5":
                holder.setVisible(R.id.ll_wait_pay, true);
                TextView tvRestTime = holder.getView(R.id.tv_rest_time);
                long leftTime = System.currentTimeMillis() / 1000 - Long.parseLong(bean.getAdd_time());
                if (leftTime > 1800)
                    listener.onOrderCancel(bean.getOrder_num(), bean.getId(), "timeout");
                if (tvRestTime.getText().toString().equals(""))
                    RxTimerUtil.interval(1, 1800 - leftTime, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext(long number) {
                            SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
                            String time = sdf.format(new Date((1800 - leftTime - number) * 1000));
                            holder.setText(R.id.tv_rest_time, String.format(ResourcesUtils.getString(R.string.order_left_time), time));
                        }

                        @Override
                        public void doComplete() {

                        }
                    });
                break;
            case "6":
                holder.setVisible(R.id.ll_wait_comment, true);
                break;
        }

        LinearLayout llContainer = holder.getView(R.id.ll_container);
        llContainer.removeAllViews();
        if (bean.getType().equals("2")) {
            List<OrderBean.ProductBean> list = bean.getProduct();
            for (int i = 0; i < list.size(); i++) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_good_or_project, llContainer, false);
                ImageView ivGoodPic = view.findViewById(R.id.iv_good_pic);
                ImageView ivSelfSupport = view.findViewById(R.id.iv_self_support);
                TextView tvGoodName = view.findViewById(R.id.tv_good_name);
                TextView tvVipPrice = view.findViewById(R.id.tv_vip_price);
                TextView tvNormalPrice = view.findViewById(R.id.tv_normal_price);

                OrderBean.ProductBean productBean = list.get(i);
                if (productBean.getPic() != null) {
                    GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + productBean.getPic().split(",")[0],
                            R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
                }
                if (productBean.getP_name() != null) {
                    tvGoodName.setText(productBean.getP_name());
                }
                if (productBean.getSelf_support() != null) {
                    ivSelfSupport.setVisibility(productBean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
                }
                tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), productBean.getPrice_vip()));
                tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), productBean.getPrice()));
                llContainer.addView(view);
            }
        } else {
            List<OrderBean.ProjectBean> list = bean.getProject();
            for (int i = 0; i < list.size(); i++) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_good_or_project, llContainer, false);
                ImageView ivGoodPic = view.findViewById(R.id.iv_good_pic);
                ImageView ivSelfSupport = view.findViewById(R.id.iv_self_support);
                TextView tvGoodName = view.findViewById(R.id.tv_good_name);
                TextView tvVipPrice = view.findViewById(R.id.tv_vip_price);
                TextView tvNormalPrice = view.findViewById(R.id.tv_normal_price);
                TextView tvProjectFit = view.findViewById(R.id.tv_project_fit);
                TextView tvProjectTime = view.findViewById(R.id.tv_project_time);

                OrderBean.ProjectBean projectBean = list.get(i);
                GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + projectBean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
                tvGoodName.setText(projectBean.getP_name());
                ivSelfSupport.setVisibility(projectBean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
                tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), projectBean.getPrice_vip()));
                tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), projectBean.getPrice()));
                tvProjectFit.setText(String.format(ResourcesUtils.getString(R.string.project_fit), projectBean.getSymptom_lists()));
                tvProjectTime.setText(String.format(ResourcesUtils.getString(R.string.project_time), projectBean.getWhen_long()));

                llContainer.addView(view);
            }
        }


        holder.setOnClickListener(R.id.ll_container, v -> {
            switch (bean.getState()) {
                case "0":
                case "1":
                case "2":
                    //type 	1:项目;2:产品
                    if (bean.getType().equals("1")) {
                        mContext.startActivity(OrderWaitServiceActivity.createIntent(mContext, bean.getId(), bean.getOrder_num()));
                    } else {
                        mContext.startActivity(OrderWaitTakeDeliveryActivity.createIntent(mContext, bean.getId(), bean.getOrder_num()));
                    }
                    break;
                case "3":
                    break;
                case "4":
                    break;
                case "5":
                    mContext.startActivity(OrderWaitPayActivity.createIntent(mContext, bean));
                    break;
                case "6":
                    mContext.startActivity(OrderWaitCommentActivity.createIntent(mContext,
                            bean.getType().equals("1") ? "project" : "product", bean.getId()));
                    break;
            }
        });

        //取消订单
        holder.setOnClickListener(R.id.tv_cancel_order, v -> {
            listener.onOrderCancel(bean.getOrder_num(), bean.getId(), "cancel");
        });

        //付款
        holder.setOnClickListener(R.id.tv_pay_order, v -> mContext.startActivity(OrderWaitPayActivity.createIntent(mContext, bean)));

        //查看快递
        holder.setOnClickListener(R.id.iv_check_logistic, v -> {
            listener.onCheckLogistics("", bean.getExpress_name(),
                    bean.getProduct().get(0).getId());
        });

        //确认收货
        holder.setOnClickListener(R.id.ic_confirm_receive, v -> {
            listener.onOrderConfirm(bean.getId());
        });
        holder.setOnClickListener(R.id.iv_evaluate, v -> {
            mContext.startActivity(OrderWaitCommentActivity.createIntent(mContext,
                    bean.getType().equals("1") ? "project" : "product", bean.getId()));
        });
    }

    private OrderBtnClickListener listener;

    public interface OrderBtnClickListener {

        void onOrderCancel(String order_num, String order_id, String type);

        void onOrderDelete(String order_num, String order_id);

        void onCheckLogistics(String address, String express_name, String order_id);

        void onOrderConfirm(String order_id);
    }

    public void setOnOrderBtnClick(OrderBtnClickListener listener) {
        this.listener = listener;
    }
}
